﻿using Script.UI;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using UnityEngine.UI;
using MTUnity.Utils;
using Script.UI.Component;
using UnityEngine;
using System;

public class UI_SortingLine : UIUpdateBase
{
	public Text Txt_CurrentSortingCountInPlan;
	public Text Txt_CurrentSortingCountInCompleted;
	public Text Txt_CurrentSortingCountPercent;
	public Text Txt_SortingDuration;
	public Text Txt_SortingEfficient;
	public Slider Sld_SortingCount;
    public Image Img_0_12Hour;
    public Image Img_13_24Hour;

    public HistogramGroup Panel_RecordSortingEfficient_7D;
    public HistogramGroup Panel_RecordSortingEfficient_14D;
    public HistogramGroup Panel_RecordSortingDuration_7D;
    public HistogramGroup Panel_RecordSortingDuration_14D;
    public HistogramGroup Panel_RecordSortingCount_7D;
    public HistogramGroup Panel_RecordSortingCount_14D;

    public Text Txt_RefreshTime;



    #region Interface
    public override Dictionary<string, UpdateDel> RegisterProcessorMethod()
    {
        JsonData json = new JsonData();

        return new Dictionary<string, UpdateDel>
        {
            {JsonFildName.SortingLine.Current_SortingLineData , UpdateCurrentSortingCount },
            {JsonFildName.SortingLine.Record_SortingCount , UpdateRecordSortingCount },
            {JsonFildName.SortingLine.Record_SortingDuration , UpdateRecordSortingDuration },
            {JsonFildName.SortingLine.Record_SortingEfficient , UpdateRecordSortingEfficient },
            {JsonFildName.SortingLine.SortingLineDataRefreshTime , UpdateRefreshTime },
        };
    }
    #endregion

    #region UpdateInterface
    public void UpdateCurrentSortingCount(JsonData jsonData)
    {
        if (jsonData.IsObject)
        {
            int iCurrentSortingCountInCompleted = JsonUtils.ToInt(jsonData[JsonFildName.SortingLine.SortingCountInCompleted]);
            int iCurrentSortingCountInPlan = JsonUtils.ToInt(jsonData[JsonFildName.SortingLine.SortingCountInPlan]);
            double dDuration = JsonUtils.ToDouble(jsonData[JsonFildName.SortingLine.SortingDuration]);
            double dEfficient = JsonUtils.ToDouble(jsonData[JsonFildName.SortingLine.SortingEfficient]);

            UpdateCurrentSortingCount(iCurrentSortingCountInCompleted , iCurrentSortingCountInPlan);
            UpdateCurrentSortingDuration(dDuration);
            UpdateSortingEfficient(dEfficient);
        }
    }

    public void UpdateRecordSortingEfficient(JsonData jsonData)
    {
        if (jsonData.IsArray)
        {
            List<HistogramDataBase> listData = Utils.SetData<HistogramData_SortingEfficient>(jsonData, JsonFildName.SortingLine.SortingEfficient , 7);
            UpdateRecordSortingEfficient(listData);
        }
    }

    public void UpdateRecordSortingDuration(JsonData jsonData)
    {
        if (jsonData.IsArray)
        {
            List<HistogramDataBase> listData =Utils.SetData<HistogramData_SortingDuration>(jsonData, JsonFildName.SortingLine.SortingDuration , 7);
            UpdateRecordSortingDuration(listData);
        }
    }

    public void UpdateRecordSortingCount(JsonData jsonData)
    {
        if (jsonData.IsArray)
        {
            List<HistogramDataBase> listData = new List<HistogramDataBase>();
            double iMax1 = 0;
            double iMax2 = 0;
            for (int i = 0; i < jsonData.Count; i++)
            {
                HistogramData_SortingCount data = new HistogramData_SortingCount()
                {
                    Current = JsonUtils.ToInt(jsonData[i][JsonFildName.SortingLine.SortingCountInCompleted]),
                    Current2 = JsonUtils.ToInt(jsonData[i][JsonFildName.SortingLine.SortingCountInPlan]),
                    Timestamp = JsonUtils.ToLong((long)jsonData[i][JsonFildName.Common.Timestamp]),
                };
                if (i < 7)
                {
                    iMax1 = Math.Max(iMax1, data.Current);
                }
                iMax2 = Math.Max(iMax2, data.Current);
                listData.Add(data);
            }
            for (int i = 0; i < listData.Count; i++)
            {
                ((HistogramData_SortingCount)listData[i]).Max = i < 7 ? iMax1 : iMax2;
            }
            UpdateRecordSortingCount(listData);
        }
    }

    public void UpdateRefreshTime(JsonData jsonData)
    {
        UpdateRefreshTime(JsonUtils.ToLong(jsonData));
    }

    #endregion

    public void UpdateCurrentSortingCount(int iSortingInCompleted , int iSortingInPlan)
    {
        Txt_CurrentSortingCountInPlan.text = string.Format("{0:N}", iSortingInPlan);
        Txt_CurrentSortingCountInCompleted.text = string.Format("{0:N}", iSortingInCompleted);

        if (iSortingInPlan == 0)
        {
            Sld_SortingCount.value = 0;
            Txt_CurrentSortingCountPercent.text = string.Format("{0}%", 0);
        }
        else
        {
            Sld_SortingCount.value = (float)iSortingInCompleted / (float)iSortingInPlan;
            Txt_CurrentSortingCountPercent.text = string.Format("{0}%", (int)(((float)iSortingInCompleted / (float)iSortingInPlan) * 100));
        }
        
        
    }

    public void UpdateCurrentSortingDuration(double dDurationTime)
    {
        float fSmallThan12Percent = dDurationTime <= 12 ? (float)dDurationTime / 12.0f : 1.0f;
        float fBigThan12Percent = dDurationTime <= 12 ? 0f : (float)(dDurationTime - 12) / 12.0f;

        Img_0_12Hour.fillAmount = fSmallThan12Percent;
        Img_13_24Hour.fillAmount = fBigThan12Percent;

        Txt_SortingDuration.text = string.Format("{0:F1}", dDurationTime);
    }

    public void UpdateSortingEfficient(double dEfficient)
    {
        Txt_SortingEfficient.text = string.Format("{0:N0}", (int)dEfficient);
    }

    public void UpdateRecordSortingEfficient(List<HistogramDataBase> listData)
    {
        Panel_RecordSortingEfficient_7D.Refresh(listData);
        Panel_RecordSortingEfficient_14D.Refresh(listData);
    }

    public void UpdateRecordSortingDuration(List<HistogramDataBase> listData)
    {
        Panel_RecordSortingDuration_7D.Refresh(listData);
        Panel_RecordSortingDuration_14D.Refresh(listData);
    }

    public void UpdateRecordSortingCount(List<HistogramDataBase> listData)
    {
        Panel_RecordSortingCount_7D.Refresh(listData);
        Panel_RecordSortingCount_14D.Refresh(listData);
    }

    public void UpdateRefreshTime(long lRefreshTime)
    {
        DateTime datatime = TimeUtil.FromTimestamp(lRefreshTime);
        Txt_RefreshTime.text = string.Format("数据更新 {0}", datatime.ToString("{yyyy-MM-dd HH:mm:ss}"));
    }
}